<?php

declare(strict_types=1);

namespace App\Controller\store;

use App\Constants\Express;
use App\Middleware\User\StoreMiddleware;
use App\Request\Store\Order\DeliverRequest;
use App\Service\Store\OrderService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 
 * 后台订单管理
 * @AutoController()
 * @Middleware(StoreMiddleware::class)
 * @author 陈奕文
 * @package App\Controller\store
 */
class OrderController extends Controller
{

    /**
     * 订单服务
     * @Inject()
     * @var OrderService
     */
    private $order_service;

    /**
     * 订单搜索
     */
    public function search()
    {
        $params = $this->request->all();

        $order_list = $this->order_service->getOrderList($params);

        $this->success($order_list);
    }

    /**
     * 获得单个订单详情
     */
    public function detail()
    {
        $id = (int)$this->request->input('id');
        !$id && $this->error("查看传输参数错误");
        // 获得订单详情
        $order_detail = $this->order_service->getDetailById($id);

        $this->success($order_detail);
    }

    /**
     * 获得单个订单的商品
     */
    public function items()
    {
        $id = (int)$this->request->input('id');
        !$id && $this->error("查看传输参数错误");
        // 获得商品
        $items = $this->order_service->order_items_dao->getItemsByOrderId($id)->toArray();

        $this->success($items);
    }

    /**
     * 订单物流发货
     * @param DeliverRequest $request
     */
    public function deliver(DeliverRequest $request)
    {
        $params = $request->all();

        $this->order_service->deliver($params);

        $this->success("发货成功");

    }

    /**
     * 物流信息修改
     * @param DeliverRequest $request
     */
    public function edit(DeliverRequest $request)
    {
        $params = $request->all();
        $this->order_service->polishInfo($params);
        $this->success("物流信息修改成功");
    }

    /**
     * 获得所有快递信息
     */
    public function allExpress()
    {
        $express_list = null;
        switch (config('express.request_type')) {
            case Express::BIRD_SUBSCRIBE:
                $express_list = $this->order_service->express_dao->getFreeExpress();
                break;

            case Express::BIRD_SUBSCRIBE_ADD_VERSION:
                $express_list = $this->order_service->express_dao->getAllExpress();
                break;
        }

        $this->success($express_list);
    }

    /**
     * 获得订单快递信息
     */
    public function expressInfo()
    {
        $order_no = $this->request->input('order_no');
        !$order_no && $this->error("请传订单编号");
        $logistic = $this->order_service->logistics_dao->getLogisticByOrderNo($order_no);
        $logistic && $logistic = $logistic->toArray();
        $this->success($logistic);
    }

}
